package com.barbeque.mall.controller;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.system.MarketPermission;
import com.barbeque.mall.bean.system.MarketRole;
import com.barbeque.mall.bean.system.MarketRoleOptions;
import com.barbeque.mall.bean.system.bo.FirstClass;
import com.barbeque.mall.bean.system.bo.MarketPermissionBO;
import com.barbeque.mall.bean.system.bo.SecondClass;
import com.barbeque.mall.bean.system.bo.ThirdClass;
import com.barbeque.mall.bean.system.vo.MarketRoleOptionsVO;
import com.barbeque.mall.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/17 11:16
 */
@RestController
@RequestMapping("admin/role")
public class SysRoleController {


    @Autowired
    SysRoleService sysRoleService;


    @RequestMapping("options")
    public BaseRespVo getAdminRole() {
        CommonData adminRole = sysRoleService.getAdminRoleOptions();
        return BaseRespVo.ok(adminRole);
    }

    @RequestMapping("list")
    public BaseRespVo getRoleList(BasePageInfo basePageInfo, String name) {
        CommonData roleList = sysRoleService.getRoleList(basePageInfo, name);

        return BaseRespVo.ok(roleList);
    }

    @RequestMapping("create")
    public BaseRespVo createRole(@RequestBody MarketRole marketRole) {
        marketRole.setEnabled(true);
        marketRole.setAddTime(new Date(System.currentTimeMillis()));
        marketRole.setUpdateTime(new Date(System.currentTimeMillis()));
        marketRole.setDeleted(false);

        int role = sysRoleService.createRole(marketRole);
        if (role == 1) {
            return BaseRespVo.ok(marketRole);
        }

        return BaseRespVo.invalidData("新增角色失败");
    }


    @RequestMapping("update")
    public BaseRespVo updateRole(@RequestBody MarketRole marketRole) {
        marketRole.setUpdateTime(new Date(System.currentTimeMillis()));
        int i = sysRoleService.updateRole(marketRole);
        if (i == 1) {
            return BaseRespVo.ok(marketRole);
        }
        return BaseRespVo.invalidData("更新失败");
    }

    @RequestMapping("delete")
    public BaseRespVo deleteRole(@RequestBody MarketRole marketRole) {
        marketRole.setDeleted(true);
        int i = sysRoleService.deleteRole(marketRole);
        if (i == 1) {
            return BaseRespVo.ok(null);

        }
        return BaseRespVo.invalidData("删除失败");
    }

    @RequestMapping(value = "permissions", method = RequestMethod.GET)
    public BaseRespVo getPermissions(Integer roleId) {
        //处理get请求

        //根据roleId查找对应的权限，封装到assignedPermissions中
        List<String> assignedPermissions = sysRoleService.getPermissionsByRoleId(roleId);


        //获取所有权限所有类目
        //获取所有pid=0的一级项
        List<MarketRoleOptions> marketRoleOptionsList = sysRoleService.getMarketRoleOptionsByPid(0);
        //进行类的转化，转成FirstClass
        ArrayList<FirstClass> systemPermissions = new ArrayList<>();


        for (MarketRoleOptions marketRoleOptions : marketRoleOptionsList) {
            FirstClass firstClass = new FirstClass();
            firstClass.setId(marketRoleOptions.getId());
            firstClass.setLabel(marketRoleOptions.getLabel());
            //根据一级类的pk作为二级类的pid来查询一级类对应的二级类，将其封装到一积累的children中
            List<MarketRoleOptions> marketRoleOptionsList2 = sysRoleService.getMarketRoleOptionsByPid(marketRoleOptions.getPrimaryKey());
            ArrayList<SecondClass> secondClasses = new ArrayList<>();
            for (MarketRoleOptions roleOptions : marketRoleOptionsList2) {
                //对其进行类型转化，转化成二级类SecondClass
                SecondClass secondClass = new SecondClass();
                secondClass.setId(roleOptions.getId());
                secondClass.setLabel(roleOptions.getLabel());


                //根据二级类的pk作为三级类的pid来查询三级类，封装到二级类的children中
                ArrayList<ThirdClass> thirdClasses = new ArrayList<>();
                List<MarketRoleOptions> marketRoleOptionsList3 = sysRoleService.getMarketRoleOptionsByPid(roleOptions.getPrimaryKey());
                for (MarketRoleOptions options : marketRoleOptionsList3) {
                    ThirdClass thirdClass = new ThirdClass();
                    thirdClass.setId(options.getId());
                    thirdClass.setLabel(options.getLabel());
                    thirdClass.setApi(options.getApi());
                    thirdClasses.add(thirdClass);
                }

                //将三级类给二级
                secondClass.setChildren(thirdClasses);
                //将二级类加入list中
                secondClasses.add(secondClass);
            }

            firstClass.setChildren(secondClasses);
            systemPermissions.add(firstClass);


        }

        MarketRoleOptionsVO marketRoleOptionsVO = new MarketRoleOptionsVO();
        marketRoleOptionsVO.setAssignedPermissions(assignedPermissions);
        marketRoleOptionsVO.setSystemPermissions(systemPermissions);
        return BaseRespVo.ok(marketRoleOptionsVO);
    }


    @RequestMapping(value = "permissions", method = RequestMethod.POST)
    public BaseRespVo editPermission(@RequestBody MarketPermissionBO marketPermissionBO) {
        //处理post请求
        //根据roleId 先删除以前所有的permission，再插入新的

        int i = sysRoleService.deleteRolePermissionByRoleId(marketPermissionBO.getRoleId());
        //根据权限数据，在表中创建新的角色权限关系
        int rolePermission = sysRoleService.createRolePermission(marketPermissionBO);

        if (rolePermission == marketPermissionBO.getPermissions().size()) {
            return BaseRespVo.ok(null);
        }
        return BaseRespVo.invalidData("编辑失败");
    }


}
